using AvalonAdmin.BaseControl;
using AvalonAdmin.Pages.Common.ViewModels;
using AvalonAdminDto.Common;
using Avalonia.Controls;
using Avalonia.Data;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Linq;
using System;

namespace AvalonAdmin.Pages.Common.Views
{
    /// <summary>
    /// Page_Import.axaml 的交互逻辑
    /// </summary>
    public partial class ImportPage : BaseUserControl
    {
        private ImportViewModel? _viewModel;
        
        public ImportPage()
        {
            InitializeComponent();
        }
        
        public ImportPage(ImportType importType = ImportType.VehicleSeries, string? id = null, ViewStatus? viewStatus = ViewStatus.Add)
        {
            InitializeComponent();
            _viewModel = new ImportViewModel(this, id, viewStatus, importType);
            this.DataContext = _viewModel;
            
            // 监听Columns集合变化，动态创建DataGrid列
            if (_viewModel.Columns != null)
            {
                _viewModel.Columns.CollectionChanged += OnColumnsChanged;
                // 如果已经有列数据，立即创建列
                if (_viewModel.Columns.Any())
                {
                    CreateDataGridColumns();
                }
            }
            
            // 监听Data集合变化
            if (_viewModel.Data != null)
            {
                _viewModel.Data.CollectionChanged += OnDataChanged;
                Console.WriteLine($"[DEBUG] 已注册Data集合变化监听器");
            }
            
            // 监听ViewModel的PropertyChanged事件
            _viewModel.PropertyChanged += OnViewModelPropertyChanged;
        }
        
        /// <summary>
        /// 当Columns集合发生变化时，重新创建DataGrid列
        /// </summary>
        private void OnColumnsChanged(object? sender, NotifyCollectionChangedEventArgs e)
        {
            CreateDataGridColumns();
        }
        
        /// <summary>
        /// 根据ViewModel中的Columns集合创建DataGrid列
        /// </summary>
        private void CreateDataGridColumns()
        {
            if (_viewModel?.Columns == null) return;
            
            // 清除现有列
            dataGrid.Columns.Clear();
            
            // 根据Columns集合创建新列
            foreach (var columnInfo in _viewModel.Columns)
            {
                var column = new DataGridTextColumn
                {
                    Header = columnInfo.Header,
                    Binding = new Binding(columnInfo.BindingPath),
                    IsReadOnly = true,
                    Width = new DataGridLength(1, DataGridLengthUnitType.Star)
                };
                
                dataGrid.Columns.Add(column);
            }
            Console.WriteLine($"[DEBUG] DataGrid列创建完成，共{dataGrid.Columns.Count}列");
        }
        
        /// <summary>
        /// 当Data集合发生变化时的处理
        /// </summary>
        private void OnDataChanged(object? sender, NotifyCollectionChangedEventArgs e)
        {
            Console.WriteLine($"[DEBUG] Data集合发生变化，操作类型: {e.Action}");
            if (_viewModel?.Data != null)
            {
                Console.WriteLine($"[DEBUG] 当前Data集合数量: {_viewModel.Data.Count}");
                
                // 强制刷新DataGrid
                Avalonia.Threading.Dispatcher.UIThread.InvokeAsync(() =>
                {
                    dataGrid.ItemsSource = null;
                    dataGrid.ItemsSource = _viewModel.Data;
                    Console.WriteLine($"[DEBUG] DataGrid ItemsSource已重新设置");
                });
            }
        }
        
        /// <summary>
        /// 当ViewModel属性发生变化时的处理
        /// </summary>
        private void OnViewModelPropertyChanged(object? sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if (e.PropertyName == nameof(_viewModel.Data))
            {
                Console.WriteLine($"[DEBUG] ViewModel的Data属性发生变化");
                
                // 重新注册Data集合变化监听器
                if (_viewModel?.Data != null)
                {
                    _viewModel.Data.CollectionChanged -= OnDataChanged;
                    _viewModel.Data.CollectionChanged += OnDataChanged;
                    Console.WriteLine($"[DEBUG] 重新注册Data集合变化监听器，当前数据量: {_viewModel.Data.Count}");
                }
            }
        }
    }
}
